<?php
class Watchdog_model extends MY_Model
{
	public
		$table = 'watchdog',
		$prKey = 'id'		
	;
	
	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id' 				=> '',
			'id_admins'		 	=> T_('Администратор'),
			'message'		 	=> T_('Сообщение'),
			'data' 				=> T_('Данные'),
			'location'		 	=> T_('Путь'),
			'referer'	 		=> T_('Реферер'),
			'date_created'		=> T_('Дата и время'),
		);
		
		$this->rules = array(
			'id_admins'			=> "trim|strip_tags|required|is_natural_no_zero",
			'message' 			=> "trim|strip_tags|required|max_length[255]",
			'data' 				=> "trim|strip_tags",		
			'location'			=> "trim|strip_tags|max_length[255]",
			'referer'			=> "trim|strip_tags|max_length[255]",
			'date_created'		=> "trim|max_length[32]",			
		);		
	}
	
	function search($config = array())
	{
		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');
		$id_user 	= $this->input->get_post('id_admin');

		$sql  = 'SELECT *';
		$sql .= ' FROM '.$this->table.' w';
		$sql .= ' WHERE 1 = 1 ';  
			
		if ($id_user)
		{
			$sql .= ' AND w.id_admins =' . $this->db->escape($id_user); 
		}
		
		$this->data['list'] = $this->db->query($sql)->result();

		$this->data['id_user'] = $id_user;
	}
	
	function clear()
	{
		
	}	
	
	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);
		
		$error = "";
		$report = "";
		foreach ($items as $id)
		{	
			$res = $this->db->delete($this->table, array($this->prKey => $id));
			$report .= ",1"; // deleted 	
			
			if (is_null($res))
			{
				$error = true;
				$this->log->writeMessage('error', T_('Error. Лог не удален.'));
			}
			else
			{
				$this->log->writeMessage('done', T_('Лог удален.'));
			}
		}
		return array("error" => $error, "report" => substr($report, 1));
	}	
	
	function insert($data)
	{
		if (!empty($data))
		{
			parent::insert($data);
		}		
	}	
}
?>